<?php
namespace Home\Model;
class Resource extends City{
    protected $redis;
    protected $user;
    protected $username;
    protected $city;
    public function __construct($user){
        parent::__construct($user,'resource');
    }
    
    //初始化资源
    public function iniResource($type_level, $type){
        $type_pre = $type_level.'_'.$type;
        $this->hMset(array($type_pre.':type_level'=>$type_level, $type_pre.':level'=>1, $type_pre.':gold'=>0,
                    $type_pre.':active'=>0, $type_pre.':active_sec'=>0, $type_pre.':active_base_ts'=>0)); 
    }
    
    public function initData(){
        //开启三个资源生产,有四种资源
        $this->iniResource('low', '铁');
        $this->iniResource('low', '木');
        $this->iniResource('low', '革');
        // $this->iniResource($user, 'low', '草');
    }

    //资源状态结余（投资状态结余)
    public function saveResource($type_level, $type, $ts){
        $type_pre = $type_level.'_'.$type;
        $info = $this->getCityInfo($type_pre);
        if($info[$type_pre.':active'] == 1){
            $surplus = $info[$type_pre.':active_base_ts'] + $info[$type_pre.':active_sec'] - $ts;
            if($surplus <= 0){
                //时间到
                $this->hMset(array($type_pre.':active'=>0, $type_pre.':active_sec'=>0, $type_pre.':active_base_ts'=>0));
                //增益消失 资源同步结余
                $this->resSaveAll($type_level, $type, $ts, $info);
            }else{
                $this->hMset(array($type_pre.':active'=>1, $type_pre.':active_sec'=>$surplus, $type_pre.':active_base_ts'=>$ts)); 
            }
        }
    } 
    
    // 资源投资
    public function goldResource($type_level, $type, $ts){
        //先更新状态
        $this->saveResource($type_level, $type, $ts);
        $type_pre = $type_level.'_'.$type;
        $config = cfg('resource');
        $info = $this->getCityInfo($type_pre);
        //单笔投资金额
        $increment = $config[$type_level]['increment'][$info[$type_pre.':level']];
        //升级所需金额
        $investment = $config[$type_level]['investment'][$info[$type_pre.':level']];
        $gold = $increment + $info[$type_pre.':gold'];
        //所需金额不足单笔投资金额即可升级,就取剩余升级金额
        if($gold >= $investment){
            $increment = $investment - $info[$type_pre.':gold'];
            $gold = 0;//升级,初始化金额为0
        }
        //减钱
        if(!$this->user->container->decMoney($increment)) return false;
        //加增益
        $surplus = $info[$type_pre.':active_sec'] + 1800;
        if($surplus > 86400) $surplus = 86400;//最大增益持续一天

        if($gold == 0){
            $this->resSaveAll($type_level, $type, $ts, $info);//资源结余
            //升级动作
            $this->hIncrBy($type_pre.':level', 1);//升等级
        }else if($info[$type_pre.':active'] == 0){
            $this->resSaveAll($type_level, $type, $ts, $info);//激活状态前 资源结余
        }
        //保存投资人的总投资
        $this->hIncrBy($type_pre.'_gold_'.$this->username, $increment);
        $this->hMset(array($type_pre.':active'=>1, $type_pre.':active_sec'=>$surplus, $type_pre.':active_base_ts'=>$ts, $type_pre.':gold'=>$gold));
        return $increment;
    }

   

    public function getCityInfo($type_pre){
        return $this->hMget(array($type_pre.':level', $type_pre.':active',$type_pre.':active_sec', $type_pre.':active_base_ts', $type_pre.':gold'));
    }

    public function resSaveAll($type_level, $type, $ts, $info){
        $users = $this->getUsers();
        foreach($users as $city_user){
            $bucket = new \Home\Model\Bucket($user);
            $bucket->username = $city_user;
            $bucket->resSaveAll($type_level, $type, $ts, $info);
        }
    }
    
}